import { createRouter, type RouteRecordRaw, createWebHistory } from 'vue-router'
import { storeToRefs } from 'pinia'
import store from '../store/index'

//路由表
const routes: Array<RouteRecordRaw> = [
    { name: 'index', path: '/', meta: { index: 0, label: '后台首页', icon: '' }, component: () => import('../views/Index.vue') },
    { name: 'login', path: '/login', meta: { index: 0, label: '登录', icon: '' }, component: () => import('../views/Login.vue') },
    {
        name: 'indexbase', path: '/index', meta: { index: 0, label: '用户管理', icon: '' }, component: () => import('../views/Index.vue'),
        children: [
            {
                name: 'base', path: '', component: () => import('../views/system/workbench.vue')
            },
            {
                name: 'workbench', path: '/system/workbench', component: () => import('../views/system/workbench.vue')
            },
            //用户
            { name: 'users', path: '/users', meta: { index: 16, label: '用户管理', icon: '' }, component: () => import('@/views/users/Index.vue') },
            
            
            { name: 'usersCreate', path: '/users/create', meta: { index: 17, label: '用户新增', icon: '' }, component: () => import('../views/users/Create.vue') },

            {name:'vip',path:'/vip/Index',meta:{index:17,label:'会员管理',icon:''},component:()=>import('@/views/vip/Index.vue')},
            { name: 'vipreport', path: '/Reportform/VipReportfrom', meta: { index: 16, label: '会员报表', icon: '' }, component: () => import('@/views/Reportform/VipReportfrom.vue') },

            { name: 'info', path: '/info', meta: { index: 15, label: '个人信息', icon: '' }, component: () => import('../views/info/Index.vue') },
             { name: 'Roles', path: '/Role', meta: { index: 44, label: '角色管理', icon: '' }, component: () => import('@/views/Roles/index.vue') },
             {name:'DisTable',path:'/DisTable',meta:{index:13,label:'可视化表',icon:''},component:()=>import('@/views/DisTable/index.vue')},
                {name:'pp',path:'/pp',meta:{index:12,label:'可视化表',icon:''},component:()=>import('@/views/DisTable/pp.vue')},
                 {name:'cc',path:'/cc',meta:{index:11,label:'可视化表',icon:''},component:()=>import('@/views/DisTable/cc.vue')},
                  {name:'menu',path:'/Menu',meta:{index:18,label:'菜单管理',icon:''},component:()=>import('@/views/Menu/index.vue')},
                     {name:'dept',path:'/Dept',meta:{index:43,label:'部门管理',icon:''},component:()=>import('@/views/Dept/Index.vue')},
                       {name:'goods',path:'/goods',meta:{index:10,label:'商品管理',icon:''},component:()=>import('@/views/goods/index.vue')},
                          {name:'GoodControl',path:'/GoodControl',meta:{index:7,label:'商品控制台',icon:''},component:()=>import('@/views/GoodControl/index.vue')},
                             {name:'spdj',path:'/control/order',meta:{index:5,label:'商品点单',icon:''},component:()=>import('@/views/OrderControl/index.vue')},
                               {name:'pder/Index',path:'/pder/Index',meta:{index:13,label:'订单报表',icon:''},component:()=>import('@/views/pder/Index.vue')},
                 
                              {name:'/pder/dinkon',path:'/pder/dinkon',meta:{index:6,label:'订单控制台',icon:''},component:()=>import('@/views/pder/dinkon.vue')},
                                { name: 'sotreinfo', path: '/storeinfo', meta: { index: 8, label: '店铺信息', icon: '' }, component: () => import('@/views/Address/index.vue') },
                                 {name:'/pder/tu',path:'/pder/tu',meta:{index:58,label:'统计图',icon:''},component:()=>import('@/views/pder/tu.vue')}

        ]
    }
]

const router = createRouter({
    routes: routes,  //路由表
    history: createWebHistory()  //模式 hash history
})

//导航守卫
router.beforeEach((to, from) => {
  //处理刷新 手动刷新和URL直接跳转
  const { addTab } = store()
  const { tabsvalue, menuactive } = storeToRefs(store())
  tabsvalue.value = to.meta.index + ''
  menuactive.value = to.meta.index + ''
  addTab({ name: to.meta.index + '', label: to.meta.label + '', url: to.path, icon: to.meta.icon + '', closable: true })



  const { token } = storeToRefs(store())
  //console.log('token: ', token.value);

  if (token.value.length > 0 || to.path == "/login") {
    return true
  } else {
    router.push('/login')
  }
})

export default router